/**
 *
 * @Author:   tiannanyihao
 * @Date:     2019-07-01 14:22
 * @Profile:  api
 * @Project:  byf

 * @Description:
 *
 */

import requestBase from './requestBase'
import Indicators from '@/utils/Indicator'
import Alert from '@/utils/Alert'
import { Toast } from 'mint-ui'

/**************************************** NETWORK_API ****************************************/

/*web_H5_ - 发送手机验证码*/
const sendSmsVerificationCode = (data) => {
  return new Promise((resolve, reject) => {
    let url = '/sdb/yifu/web/sms/sendSmsVerificationCode'
    data= JSON.stringify(data);
    post(url, data, resolve, reject)
  })
}

/*web_H5_ - 查询交易结果*/
const queryTransactionResult = (data) => {
  return new Promise((resolve, reject) => {
    let url = '/sdb/yifu/web/transfer/queryTransactionResult'
    data= JSON.stringify(data);
    post(url, data, resolve, reject)
  })
}

/*web_H5 - 查询卡信息*/
const queryCardInfo = (data) => {
  return new Promise((resolve, reject) => {
    let url = '/sdb/yifu/web/card/queryCardInfo'
    data= JSON.stringify(data);
    post(url, data, resolve, reject)
  })
}

/*web_H5_ - 查询可转账限额*/
const getTransferQuota = (data) => {
  return new Promise((resolve, reject) => {
    let url = '/sdb/yifu/web/quota/getTransferQuota'
    data= JSON.stringify(data);
    post(url, data, resolve, reject)
  })
}

/*web_H5_ - 获取token接口*/
const getToken = (data) => {
  return new Promise((resolve, reject) => {
    let url = '/sdb/yifu/web/token/getToken'
    data= JSON.stringify(data);
    post(url, data, resolve, reject)
  })
}

/*web_H5_ - 转账-他人银行卡接口*/
const transferOtherBank = (data) => {
  return new Promise((resolve, reject) => {
    let url = '/sdb/yifu/web/transfer/transferOtherBank'
    data= JSON.stringify(data);
    post(url, data, resolve, reject)
  })
}

const getDemoFor111Test=(data)=>{
  return new Promise((resolve, reject)=>{
    let url = 'http://172.28.247.111:8071/'
    data= JSON.stringify(data);
    get(url,data,resolve,reject)
  })
}

/**************************************** NETWORK_TOOL ****************************************/

/**
 * 公共post方法
 * @param url 地址
 * @param data 参数对象{}
 * @param resolve 成功
 * @param reject 失败
 * @param isFormData 是否以form表单方式发送,默认以form表单方式发送
 */
const post = (url, data, resolve, reject, isFormData = true) => {

  requestBase.post(url, data)
    .then((res) => {
      /*非200状态*/
      if (res.status !== 200) {
        reject({msg: 'status is not 200', code: '009'})
      }
      let data = res.data
      let code = data.code

      /*500错误*/
      if (code === 500) {
        MyToast(data.msg)
        reject(data)
      }
      else {
        resolve(data.data)
      }
    }).catch((err) => {
    reject(err)
  })
}

const get = (url,data,resolve,reject)=>{
  requestBase.get(url,data).then((res)=>{
    if (res.status !== 200){
      reject({msg:'status is not 200',code:'009'})
    }
    resolve(res)

  }).catch((err)=>{
    reject(err)
  })
}


/**************************************** UI_TOOL ****************************************/

/*启动指示器*/
const showIdc = (msg) => {
  Indicators.show(msg)
}
/*关闭指示器*/
const hideIdc = () => {
  Indicators.hidden()
}

/*Toast弹框*/
const MyToast = (msg, delayTime = 1800, position = 'center') => {
  Toast({
    message: msg,
    position: position,
    duration: delayTime,
  })
}

/*alert弹框(仅确认)*/
const alertSure = (title, msg, callBack) => {
  Alert.alertSure(title, msg, callBack)
}
/*alert弹框(取消+确认)*/
const alertChoose = (title, msg, cancleTitle, suerTitle, callBack) => {
  Alert.alertChoose(title, msg, cancleTitle, suerTitle, callBack)
}

export default {
  /*Indicator功能接口*/
  showIdc,
  hideIdc,

  /*MwssageBox功能接口*/
  alertSure,
  alertChoose,

  /*测试接口*/
  getDemoFor111Test, //开发阶段, 访问demo地址,以便从后端获取token/public_key

  /*api接口*/
  sendSmsVerificationCode,
  queryTransactionResult,
  queryCardInfo,
  getTransferQuota,
  getToken,
  transferOtherBank,
}
